﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using PolePositionMobileUtilityEntity;
using PolePositionMobileUtilityGeneral;

namespace PolePositionMobileUtilityApplication.Controls
{
    public partial class AssignNotes : UserControl
    {
        private List<Int32> _ValuesParentNote; 
        private List<Int32> _ValuesChildNote; 

        public AssignNotes()
        {
            InitializeComponent();
            _ValuesParentNote = new List<Int32>();
            _ValuesChildNote = new List<Int32>();
        }

        public void LoadCombos()
        {
            ENote n = new ENote();
            ENote[] nArray = n.GetAll();
            _ValuesParentNote = MobileUIHelper.FillListbox(lstParentNotes, nArray, "Name", "Id", "");

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int parentValue;
            if (lstParentNotes.SelectedIndex >= 0)
            { 
                parentValue = _ValuesParentNote[lstParentNotes.SelectedIndex];

                if (!_ValuesChildNote.Contains(parentValue))
                {
                    _ValuesChildNote.Add(parentValue);
                    lstChildNotes.Items.Add(lstParentNotes.SelectedItem);
                }
            }
        }

        private void bntRemove_Click(object sender, EventArgs e)
        {
            if (lstChildNotes.SelectedIndex >= 0)
            {
                _ValuesChildNote.RemoveAt(lstChildNotes.SelectedIndex);
                lstChildNotes.Items.RemoveAt(lstChildNotes.SelectedIndex);
            }
        }

        public List<ENote> GetSelectedValues()
        {
            List<ENote> noteList = new List<ENote>();
            ENote note;
            foreach (int i in _ValuesChildNote)
            {
                note = new ENote(i, "bla");
                noteList.Add(note);
            }

            return noteList;
        }

        public void SetSelectedValues(List<ENote> selectedValues)
        {
            Clean();
            foreach (ENote  n in selectedValues)
            {
                if (_ValuesParentNote.Contains(n.Id))
                {
                    _ValuesChildNote.Add(n.Id);
                    lstChildNotes.Items.Add(lstParentNotes.Items[_ValuesParentNote.IndexOf(n.Id)]);
                }
            }
        }

        public void Clean()
        {
            _ValuesChildNote.Clear();
            lstChildNotes.Items.Clear();
        }
    }
}
